package com.zuihai.app.mvp.ui.adapter;

import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import com.jess.arms.base.BaseFragment;
import com.zuihai.app.mvp.ui.widget.behavior.listener.IsChildScrollListener;
import com.zuihai.app.mvp.ui.widget.behavior.listener.NeedExpandListener;
import com.zuihai.app.mvp.ui.widget.behavior.listener.SupportNeedExpendListener;

import java.lang.ref.WeakReference;
import java.util.List;

public class FindPagerAdapter extends FragmentStatePagerAdapter implements SupportNeedExpendListener, IsChildScrollListener {
    private List<BaseFragment> list;
    private CharSequence[] mTitles;
    private WeakReference<ViewPager> mViewPagerReference;
    private NeedExpandListener mNeedExpandListener;

    public FindPagerAdapter(FragmentManager fm, ViewPager viewPager) {
        super(fm);
        mViewPagerReference = new WeakReference<>(viewPager);
    }


    public void bindData(List<BaseFragment> list) {
        this.list = list;
        notifyDataSetChanged();

    }

    public void bindData(List<BaseFragment> list, CharSequence[] titles) {
        this.list = list;
        this.mTitles = titles;
        notifyDataSetChanged();
    }

    @Override
    public Fragment getItem(int position) {
        return list.get(position);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (mTitles != null) {
            return mTitles[position];
        }
        return super.getPageTitle(position);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void setNeedExpendListener(NeedExpandListener listener) {
        mNeedExpandListener = listener;
        setFragmentNeedExpandListener();
    }

    @Override
    public NeedExpandListener getNeedExpendListener() {
        return mNeedExpandListener;
    }

    private void setFragmentNeedExpandListener(){
        for (Fragment fragment : list) {
            if (fragment instanceof SupportNeedExpendListener) {
                ((SupportNeedExpendListener) fragment).setNeedExpendListener(mNeedExpandListener);
            }
        }
    }

    @Override
    public boolean childScroll(boolean mUp, boolean refresnEnable) {
        int currentItem = mViewPagerReference.get().getCurrentItem();
        Fragment fragment = getItem(currentItem);
        if (fragment instanceof IsChildScrollListener){
            return ((IsChildScrollListener) fragment).childScroll(mUp, refresnEnable);
        }
        return false;
    }

//    @Override
//    public Object instantiateItem(ViewGroup container, int position) {
//        Fragment f = (Fragment) super.instantiateItem(container, position);
//        View view = f.getView();
//        if (view != null)
//            container.addView(view);
//        return f;
//    }
//
//    @Override
//    public void destroyItem(ViewGroup container, int position, Object object) {
//        View view = list.get(position).getView();
//        if (view != null)
//            container.removeView(view);
//    }
}
